package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.TaskNumBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class TaskActivity extends BaseActivity implements View.OnClickListener {
    private TextView createNum, distributedNum, participateNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task);
        initTitle();
        initView();
        setListener();
    }

    private void setListener() {
        findViewById(R.id.create).setOnClickListener(this);
        findViewById(R.id.distributed).setOnClickListener(this);
        findViewById(R.id.participate).setOnClickListener(this);
        findViewById(R.id.add_task).setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        upLoadGetNum();
        super.onResume();
    }

    TaskNumBean.InfoEntity infos = new TaskNumBean.InfoEntity();

    private void upLoadGetNum() {
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("token", MyApplication.getInstance().getUserBean().getToken());

        } catch (Exception e) {
            MyToast("重新登录");
            return;
        }
        showDialog();

        XUtil.Post(Url.TASK_GETTASKINDEX, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        TaskNumBean bean = gson.fromJson(result, TaskNumBean.class);
                        infos = bean.getInfo();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                setNum();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    private void setNum() {
        createNum.setText(infos.getCreate_sum());
        distributedNum.setText(infos.getSend_sum());
        participateNum.setText(infos.getPart_sum());
    }

    private void initView() {
        createNum = (TextView) findViewById(R.id.createNum);
        distributedNum = (TextView) findViewById(R.id.distributedNum);
        participateNum = (TextView) findViewById(R.id.participateNum);

    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("任务");
        title.setTextColor(getResources().getColor(R.color.main_color));
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            //我创建的
            case R.id.create:
                intent = new Intent(this, TaskAllActivity.class);
                intent.putExtra("type", 0);
                intent.putExtra("title", "我创建的");
                break;
            //我派发的
            case R.id.distributed:
                intent = new Intent(this, TaskAllActivity.class);
                intent.putExtra("type", 1);
                intent.putExtra("title", "我派发的");
                break;
            //我参与的
            case R.id.participate:
                intent = new Intent(this, TaskAllActivity.class);
                intent.putExtra("type", 2);
                intent.putExtra("title", "我参与的");
                break;
            //添加
            case R.id.add_task:
                intent = new Intent(this, AddTaskActivity.class);
                break;
        }
        if (intent != null) {
            startActivity(intent);
        }

    }
}
